package com.itcast.demo_one.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.RecyclerView
import com.itcast.demo_one.R

// 数据类
data class PageData(val color: String, val text: String)

// 页面数据：颜色 + 文本
val pagesList = listOf(
    PageData("#FF5733", "首页"),
    PageData("#33FF57", "分类"),
    PageData("#3357FF", "消息"),
    PageData("#5733FF", "我的")
)

class ColorAdapter(): RecyclerView.Adapter<ColorAdapter.PageViewHolder>()  {

    // 创建ViewHolder
    class PageViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.pageText)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): PageViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_page, parent, false)
        return PageViewHolder(view)
    }

    override fun onBindViewHolder(
        holder: PageViewHolder,
        position: Int
    ) {
        val pageData = pagesList[position]
        with(holder){
            textView.text = pageData.text
            itemView.setBackgroundColor(pageData.color.toColorInt())
        }
    }

    override fun getItemCount(): Int {
        return pagesList.size
    }
}